package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.entity.Dish;
import org.example.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO 类描述
 *
 * @author shaoxuan.
 * @date 2023/12/23 20:33
 */
@Slf4j
@RestController
@RequestMapping("/dish")
@Api(tags = "菜品管理")

public class DishController {

    @Autowired
    private DishService service;

    /**
     * 根据店家ID查询该店家的菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/getDish/{businessId}")
    @ApiOperation(value = "根据店家ID查询该店家的菜品")

    public R<List<Dish>> getDish(@PathVariable("businessId") String id) {
        List<Dish> list = service.list(new LambdaQueryWrapper<Dish>().eq(Dish::getBusinessId, id));
        return R.success(list);
    }

    /**
     * 新增菜品
     *
     * @param dish
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增菜品")

    public R<Dish> add(@RequestBody Dish dish) {
        if (service.save(dish)) {
            return R.success(dish).setMsg("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 修改菜品  需要传入菜品ID
     *
     * @param dish
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改菜品 需要传入菜品ID")

    public R<String> update(@RequestBody Dish dish) {
        if (service.updateById(dish)) {
            return R.success("修改成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 删除菜品
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除菜品")

    public R<String> delete(@PathVariable("id") String id) {
        if (service.removeById(id)) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

}
